      <h1 class="entry-title entry-title-single" itemprop="headline">自定义ALV Grid 通用模板(ALV Grid Template)</h1>

    <div class="entry-content entry-content-single entry-content-aside" itemprop="text">
      <p><span class='wp_keywordlink_affiliate'><a href="http://www.sapjx.com/tag/alv" title="查看ALV中的全部文章" target="_blank">ALV</a></span> 通用模板</p>
<p>在项目中，如若定制的Report开发量较大时，<span class='wp_keywordlink_affiliate'><a href="http://www.sapjx.com/tag/alv" title="查看ALV中的全部文章" target="_blank">ALV</a></span>的通用模板则能起到相应的作用；</p>
<p>如若需要开发的Report较少时，还是不适宜用该模板；</p>
<p>&nbsp;</p>
<p><b>1.添加自定义 ALV 工具栏</b></p>
<p>首先，需要为ALV模板创建标准工具栏，新建<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="Function" target="_blank">Function</a></span> Module：<strong>ZCA_ALV_PRINT_SELECTION</strong></p>
<p>然后，通过标准程序拷贝相应的ALV工具栏；</p>
<p>具体操作可以查看前面笔者的愚作：《<a href="http://www.sapjx.com/reuse_alv_grid_display_lvc.html#i_callback_pf_status_set" target="_blank">REUSE_ALV_GRID_DISPLAY_LVC函数输入参数属性的应用</a>》中<a href="http://www.sapjx.com/reuse_alv_grid_display_lvc.html#i_callback_pf_status_set" target="_blank">自定义工具栏</a>的内容。</p>
<p><span id="more-637"></span></p>
<p>拷贝成功后，在空白的工具栏上，加上扩展的按钮Button1~Button5：</p>
<div id="attachment_638" style="width: 841px" class="wp-caption alignnone"><img src="http://www.sapjx.com/wp-content/uploads/2014/08/04.alv_template_application_toolbar.jpg" alt="Application Toolbar" width="831" height="350" class="size-full wp-image-638" /><p class="wp-caption-text">Application Toolbar</p></div>
<p>对应的<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="Function" target="_blank">Function</a></span> Code：BUTTON1~BUTTON5，Field Name：GW_BUTTON1~GW_BUTTON5；</p>
<div id="attachment_639" style="width: 656px" class="wp-caption alignnone"><img src="http://www.sapjx.com/wp-content/uploads/2014/08/04.alv_template_function_attributes.jpg" alt="Function Attributes" width="646" height="250" class="size-full wp-image-639" /><p class="wp-caption-text"><span class='wp_keywordlink_affiliate'><a href="http://www.sapjx.com/tag/function" title="查看Function中的全部文章" target="_blank">Function</a></span> Attributes</p></div>
<p>&nbsp;</p>
<p><b>2. 引入类型池，定义全局变量</b></p>
<p>在 ZCA_ALV_PRINT_SELECTIONTOP 上贴上在 <span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="Function Module" target="_blank">Function Module</a></span> 中将要用到的类型池及其相关变量等内容：</p>
<pre class="brush: abap;">

FUNCTION-POOL zca_alv_print_selection.      "MESSAGE-ID ..
TABLES:
  rsvar,
  dfies,
  trdir.

TYPE-POOLS:
  rsds ,
  syldb,
  sydb0.

TYPE-POOLS:
  icon,
  sym,
  slis.

*** Types definition
TYPES:
  BEGIN OF ty_psel,
    kind       TYPE flag,
    slnam      TYPE char8,
    text       TYPE char255,
    sign       TYPE tvarv_sign,
    option     TYPE tvarv_opti,
    low        TYPE tvarv_val,
    high       TYPE tvarv_val,
  END OF ty_psel.

CONSTANTS:
  cn_$cross       TYPE flag       VALUE 'X',
  cn_$null_dt     TYPE char10     VALUE '00.00.0000',
  cn_$alv_hd      TYPE i          VALUE '25',
  cn_types        TYPE c          VALUE 'S',
  cn_typed        TYPE c          VALUE 'D',
  cn_entry(4)     TYPE c          VALUE '?...',
  cn_dtype_d(4)   TYPE c          VALUE 'DATE',
  cn_dtype_n(4)   TYPE c          VALUE 'NUMC',
  cn_type_s(4)    TYPE c          VALUE 'STRG',
  cn_part(4)      TYPE c          VALUE 'SSCR',
  cn_finance(3)   TYPE c          VALUE 'ZFI',
  cn_porcument(3) TYPE c          VALUE 'ZMM'.

*** Variable, internal table and work area definition
DATA:
  gv_number       TYPE i,
  gv_decimal      TYPE tfmbbwhr2,
  datum           TYPE datum,
  gv_date         TYPE char10,
  gv_char         TYPE vtext_bf,                            "text45,
  gv_header_lines TYPE i,
  gv_show_bt6     TYPE flag,
  gt_header       TYPE slis_t_listheader,
  gt_prn_header   TYPE slis_t_listheader,
  gw_line         TYPE slis_listheader,
  gv_time         TYPE char5,
  gv_wa_line      TYPE text60,
  gv_text         TYPE text60,
  gw_button1      TYPE smp_dyntxt,
  gw_button2      TYPE smp_dyntxt,
  gw_button3      TYPE smp_dyntxt,
  gw_button4      TYPE smp_dyntxt,
  gw_button5      TYPE smp_dyntxt,
  gw_button6      TYPE smp_dyntxt,
  gt_extab        TYPE slis_t_extab , "OCCURS 0 WITH HEADER LINE,
  gt_temp_sel     TYPE ty_psel    OCCURS 0 WITH HEADER LINE,
  gt_temp_sel_dyn TYPE ty_psel    OCCURS 0 WITH HEADER LINE,
  gt_sel_tab      TYPE rsparams   OCCURS 0 WITH HEADER LINE,
  gt_pg_txtpool   TYPE textpool   OCCURS 0 WITH HEADER LINE,
  gt_l_selctab    TYPE rsscr      OCCURS 0 WITH HEADER LINE,
  gt_t_dfies      TYPE dfies      OCCURS 0 WITH HEADER LINE.

DATA :
  gv_databeg    TYPE  begda,
  gv_dataend    TYPE  endda.


* Macros
DEFINE format_date.
* &#038;1 - low
* &#038;2 - high
* &#038;3 - flag to get data selection period
  if not &#038;3 is initial.
    gv_databeg = &#038;1.
    gv_dataend = &#038;2.
  endif.
  datum = &#038;1.
  call 'DATE_CONV_INT_TO_EXT' id 'DATINT' field datum
                              id 'DATEXT' field &#038;1.
  datum = &#038;2.
  call 'DATE_CONV_INT_TO_EXT' id 'DATINT' field datum
                              id 'DATEXT' field &#038;2.
END-OF-DEFINITION.

DEFINE add_itab.
* &#038;1 - t_sel / t_sel_dyn / t_data
  append &#038;1.
  clear &#038;1.
END-OF-DEFINITION.



DEFINE header.
* &#038;1 - line
* &#038;2 - clear field
  append &#038;1 to gt_header.
  case &#038;2.
    when 'I'.        "info
      clear &#038;1-info.
    when 'K'.        "key
      clear: &#038;1-key,
             &#038;1-info.
  endcase.
END-OF-DEFINITION.
</pre>
<p>&nbsp;</p>
<p><b>3. 创建相应功能 FORM</b></p>
<p>1）<b>FRM_GET_SELSCREEN_ICON</b>，用于与标准ALV匹配Select-Option操作符：</p>
<p>2）<b>FRM_APPEND_SELTAB</b>，替换或更改Select Table；</p>
<p>3）<b>FRM_PREPARE_HEADER</b>，ALV抬头信息</p>
<p>4）<b>FRM_PF_STATUS</b>：ALV工具栏</p>
<p>5）<b>FRM_FORMAT_VAL</b>：格式化日期类型值、数值</p>
<p>&nbsp;</p>
<p>代码如下：</p>
<pre class="brush: abap;">

*&#038;---------------------------------------------------------------------*
*&#038;  Include           LZCA_ALV_PRINT_SELECTIONF01
*&#038;---------------------------------------------------------------------*
*&#038;---------------------------------------------------------------------*
*&#038;      Form  FRM_GET_SELSCREEN_ICON
*       Change Standard SAP displayed to map PM2S requirements
*&#038;---------------------------------------------------------------------*
FORM frm_get_selscreen_icon USING pr_sign   LIKE rsparams-sign
                                  pr_option LIKE rsparams-option
                         CHANGING p_icon TYPE char40.
  CASE pr_sign.
    WHEN 'I'.
      CASE pr_option.
        WHEN 'EQ'.
          MOVE '' TO p_icon.
        WHEN 'NE'.
          MOVE 'Not' TO p_icon.
        WHEN 'GT'.
          MOVE '>' TO p_icon.
        WHEN 'LT'.
          MOVE '<' TO p_icon.
        WHEN 'GE'.
          MOVE '>=' TO p_icon.
        WHEN 'LE'.
          MOVE '<=' TO p_icon.
        WHEN 'BT'.
          MOVE '' TO p_icon.
        WHEN 'NB'.
          MOVE 'Not' TO p_icon.
        WHEN 'CP'.
          MOVE '' TO p_icon.
        WHEN 'NP'.
          MOVE 'Not' TO p_icon.
      ENDCASE.

*****exclusive (RED)
    WHEN 'E'.
      CASE pr_option.
        WHEN 'EQ'.
          MOVE 'Not' TO p_icon.
        WHEN 'NE'.
          MOVE '' TO p_icon.
        WHEN 'GT'.
          MOVE text-s33 TO p_icon.
        WHEN 'LT'.
          MOVE text-s32 TO p_icon.
        WHEN 'GE'.
          MOVE text-s30 TO p_icon.
        WHEN 'LE'.
          MOVE text-s29 TO p_icon.
        WHEN 'BT'.
          MOVE text-s31 TO p_icon.
        WHEN 'NB'.
* Not exclude these range means include these
          MOVE '' TO p_icon.
        WHEN 'CP'.
          MOVE text-s31 TO p_icon.
        WHEN 'NP'.
          MOVE '' TO p_icon.
      ENDCASE.
  ENDCASE.
ENDFORM.                    "FRM_GET_SELSCREEN_ICON

*&#038;---------------------------------------------------------------------*
*&#038;  Include           LZCA_ALV_PRINT_SELECTIONF02
*&#038;---------------------------------------------------------------------*
*&#038;---------------------------------------------------------------------*
*&#038;      Form  fRM_append_seltab
*&#038;---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_append_seltab TABLES pr_temp_sel
                       STRUCTURE gt_temp_sel.
  DATA: lv_icon(40) TYPE c,
        lv_len      TYPE i,
        lv_lines    TYPE i,
        lv_start    TYPE i.
  DATA: lt_temp_header        TYPE slis_t_listheader.
  CONSTANTS: lc_dis TYPE i VALUE 3.


  lv_lines = LINES( pr_temp_sel ).
  IF lv_lines > 15 AND sy-batch = 'X'.
**** No of lines to be printed in top-of-page are too many for
****background mode.
*** Write horizontally
    lt_temp_header[] = gt_header[].
*    SORT pr_temp_sel BY text sign option.
    LOOP AT pr_temp_sel.
      AT NEW text.
        gw_line-key = pr_temp_sel-text.
        CONCATENATE gw_line-key ':' INTO gw_line-key.
      ENDAT.
      CASE pr_temp_sel-option.
        WHEN 'EQ'.
          IF pr_temp_sel-slnam EQ 'ZVSCREEN'. "Don't change 'EQ' to 'to'
            CONCATENATE pr_temp_sel-low 'EQ' pr_temp_sel-high
                   INTO gv_text SEPARATED BY space.
          ELSE.
            IF NOT pr_temp_sel-high IS INITIAL.
              CONCATENATE pr_temp_sel-low 'to' pr_temp_sel-high
                     INTO gv_text SEPARATED BY space.
            ELSE.
              gv_text = pr_temp_sel-low.
            ENDIF.
          ENDIF.
        WHEN space.
          IF pr_temp_sel-kind = 'S'.
            CONTINUE.
*          text = 'Nothing specified'.
          ELSEIF pr_temp_sel-kind = 'P'.
            gv_text =  pr_temp_sel-low.
            IF NOT pr_temp_sel-high IS INITIAL.
              CONCATENATE pr_temp_sel-low pr_temp_sel-high
                     INTO gv_text SEPARATED BY space.
            ENDIF.
          ENDIF.
        WHEN OTHERS.
          IF NOT pr_temp_sel-low IS INITIAL AND
             NOT pr_temp_sel-high IS INITIAL.
            IF pr_temp_sel-option EQ 'BT'.
              CONCATENATE pr_temp_sel-low 'to' pr_temp_sel-high
                     INTO gv_text SEPARATED BY space.
            ELSE.
              CONCATENATE pr_temp_sel-low
                          pr_temp_sel-option
                          pr_temp_sel-high
                     INTO gv_text SEPARATED BY space.
            ENDIF.
          ELSEIF NOT pr_temp_sel-low IS INITIAL AND
                 pr_temp_sel-high IS INITIAL.
            gv_text = pr_temp_sel-low.
          ELSEIF pr_temp_sel-low IS INITIAL AND
                 NOT pr_temp_sel-high IS INITIAL.
            gv_text = pr_temp_sel-high.
          ENDIF.
      ENDCASE.
      CONDENSE gv_text.
      lv_len = STRLEN( gv_text ).
      IF lv_start IS INITIAL.
        gw_line-info+7 = gv_text.
        lv_start = 7 + lv_len + lc_dis.
      ELSE.
        lv_lines = lv_start + lv_len.
        IF lv_lines < 60.
          gw_line-info+lv_start = gv_text.
          lv_start = lv_start + lv_len + lc_dis.
        ELSE.
          PERFORM frm_get_selscreen_icon USING pr_temp_sel-sign
                                               pr_temp_sel-option
                                      CHANGING lv_icon.

          gw_line-info+0(6) = lv_icon.
*      line-info+7 = text.
          header gw_line 'K'.
          gw_line-info+7 = gv_text.
          lv_start = 7 + lv_len + lc_dis.
          CLEAR gv_text.
          CONTINUE.
        ENDIF.
      ENDIF.
      AT END OF option.
*    CHECK NOT text IS INITIAL.
        PERFORM frm_get_selscreen_icon USING pr_temp_sel-sign
                                             pr_temp_sel-option
                                    CHANGING lv_icon.

        gw_line-info+0(6) = lv_icon.
*        line-info+ = text.
        header gw_line 'K'.
        CLEAR lv_start.
      ENDAT.
      CLEAR gv_text.
    ENDLOOP.

    lv_lines = LINES( gt_header ).
    IF lv_lines > 25.
      gt_header[] = lt_temp_header[].
*      line-info+0(6) = l_icon.
*        line-info+ = text.
      gw_line-key = text-t02.
      gw_line-info = text-t01.
      header gw_line 'K'.
*        CONCATENATE line-key ':' INTO line-key.

    ENDIF.

  ELSE.
*    SORT pr_temp_sel BY text.
    LOOP AT pr_temp_sel.
      AT NEW text.
        gw_line-key = pr_temp_sel-text.
        CONCATENATE gw_line-key ':' INTO gw_line-key.
      ENDAT.
      CASE pr_temp_sel-option.
        WHEN 'EQ'.
          IF pr_temp_sel-slnam EQ 'ZVSCREEN'. "Don't change 'EQ' to 'to'
            CONCATENATE pr_temp_sel-low 'EQ' pr_temp_sel-high
              INTO gv_text SEPARATED BY space.
          ELSE.
            IF NOT pr_temp_sel-high IS INITIAL.
              CONCATENATE pr_temp_sel-low 'to' pr_temp_sel-high
                INTO gv_text SEPARATED BY space.
            ELSE.
              gv_text = pr_temp_sel-low.
            ENDIF.
          ENDIF.
        WHEN space.
          IF pr_temp_sel-kind = 'S'.
            CONTINUE.
*          text = 'Nothing specified'.
          ELSEIF pr_temp_sel-kind = 'P'.
            gv_text =  pr_temp_sel-low.
            IF NOT pr_temp_sel-high IS INITIAL.
              CONCATENATE pr_temp_sel-low pr_temp_sel-high
                INTO gv_text SEPARATED BY space.
            ENDIF.
          ENDIF.
        WHEN OTHERS.
          IF NOT pr_temp_sel-low IS INITIAL
             AND NOT pr_temp_sel-high IS INITIAL.
            IF pr_temp_sel-option EQ 'BT'.
              CONCATENATE pr_temp_sel-low 'to' pr_temp_sel-high
                INTO gv_text SEPARATED BY space.
            ELSE.
              CONCATENATE pr_temp_sel-low
                          pr_temp_sel-option
                          pr_temp_sel-high
                     INTO gv_text SEPARATED BY space.
            ENDIF.
          ELSEIF NOT pr_temp_sel-low IS INITIAL AND
                 pr_temp_sel-high IS INITIAL.
            gv_text = pr_temp_sel-low.
          ELSEIF pr_temp_sel-low IS INITIAL AND
             NOT pr_temp_sel-high IS INITIAL.
            gv_text = pr_temp_sel-high.
          ENDIF.
      ENDCASE.
      CONDENSE gv_text.
*    CHECK NOT text IS INITIAL.
      PERFORM frm_get_selscreen_icon USING pr_temp_sel-sign
                                           pr_temp_sel-option
                                  CHANGING lv_icon.
      gw_line-info = lv_icon.
      gw_line-info+7 = gv_text.
      header gw_line 'K'.
      CLEAR gv_text.
    ENDLOOP.
  ENDIF.
ENDFORM.                    " frm_append_seltab

*&#038;--------------------------------------------------------------------*
*&#038;      Form  frm_prepare_header
*&#038;--------------------------------------------------------------------*
FORM frm_prepare_header TABLES pr_t_header_add
                               pr_t_sel
                               pr_t_sel_dyn
                               pr_t_header
                         USING pr_title      TYPE sytitle
                               pr_repid      TYPE repid
                               pr_tfill      TYPE char8
                               pr_logo       TYPE char30
                               pr_alv_header TYPE flag
                         value(pv_dont_show_initial) TYPE char1
                         value(pv_no_conf) TYPE xfeld.

  DATA: lt_sscr TYPE TABLE OF rsscr,
        lw_sscr TYPE rsscr.
  DATA: lv_found TYPE c,
        lo_dref TYPE REF TO data.
  FIELD-SYMBOLS: <fs> TYPE ANY.

  LOAD REPORT pr_repid PART cn_part INTO lt_sscr.  "SSCR

  CLEAR: gw_line,
  gt_header[].

** Header
  gw_line-typ = 'H'.
  gw_line-info  = pr_title.
  header gw_line 'I'.
*
* Selection values
  CLEAR gw_line.
  gw_line-typ = 'S'.

* deparment name
  gw_line-key = text-s34.
  gv_wa_line  = text-s35.
  gw_line-info = gv_wa_line.
  header gw_line 'K'.

* system name
  gw_line-key = text-s36.
  gv_wa_line  = text-s37.
  gw_line-info = gv_wa_line.
  header gw_line 'K'.

* (ERP) System
  gw_line-key = text-s38.
  CASE  pr_repid+0(3).
    WHEN cn_finance.  "'ZFI'.
      gv_wa_line = text-s39.
    WHEN cn_porcument ."'ZMM'.
      gv_wa_line = text-s40.
    WHEN OTHERS.
      gv_wa_line = text-s41.
  ENDCASE.
  gw_line-info = gv_wa_line.
  header gw_line 'K'.

* execute time
  WRITE: sy-datum TO gv_date .
  gw_line-key = text-s03.
  gv_wa_line = gv_date.
  gw_line-info = gv_wa_line.
  header gw_line 'K'.

* person who execute the program
  gw_line-key = text-s04.
  gv_wa_line = sy-uname.
  gw_line-info = gv_wa_line.
  header gw_line 'K'.


  IF NOT gt_temp_sel_dyn[] IS INITIAL.
    gw_line-key = '.'.
    header gw_line 'I'.
    gw_line-key = text-s06.
    CLEAR gw_line-info.
    header gw_line 'K'.
*    print_seltab gt_temp_sel_dyn.
    PERFORM frm_append_seltab TABLES gt_temp_sel_dyn. "USING lt_sscr.
  ENDIF.

* Remove the initial parameter values
  IF NOT pv_dont_show_initial IS INITIAL.
*** Delete select-options with blank values.
    DELETE gt_temp_sel WHERE kind = 'S' AND
                       sign IS INITIAL AND
                       option IS INITIAL.
*** Delete parameters with blank values
    LOOP AT gt_temp_sel WHERE kind = 'P'
                    AND low CO '0 '.
*** Get the data type
      READ TABLE lt_sscr WITH KEY name = gt_temp_sel-slnam
                         INTO lw_sscr.
*** STRING cannot be assigned dynamically. Check explicitly.
      IF lw_sscr-dtyp = cn_type_s . ""'STRG'.
        IF gt_temp_sel-low IS INITIAL.
          DELETE gt_temp_sel.
        ENDIF.
        CONTINUE.
      ENDIF.
      CREATE DATA lo_dref TYPE (lw_sscr-type).
      ASSIGN lo_dref->* TO <fs>.
      MOVE gt_temp_sel-low TO <fs> .
*    ASSIGN p_sel-low TO <fs> CASTING TYPE (p_sscr-type).
      IF <fs> IS INITIAL.
        DELETE gt_temp_sel.
      ENDIF.
      UNASSIGN <fs>.
    ENDLOOP.
  ENDIF.
  gw_line-typ  = 'S'.
  gw_line-key  = text-s01.
  gw_line-info = space.
  header gw_line 'K'.
  PERFORM frm_append_seltab TABLES gt_temp_sel.

* Additional Header
  IF NOT pr_t_header_add[] IS INITIAL.
    gt_temp_sel[] = pr_t_header_add[].
*    print_seltab gt_temp_sel.
    PERFORM frm_append_seltab TABLES gt_temp_sel.
  ENDIF.
*
  IF pr_alv_header IS INITIAL.
    pr_t_sel[]     = gt_temp_sel[].
    pr_t_sel_dyn[] = gt_temp_sel_dyn[].
    pr_t_header[]  = gt_header[].
    EXIT.
  ENDIF.
*
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      i_logo             = pr_logo
      it_list_commentary = gt_header.

  DESCRIBE TABLE gt_header LINES gv_header_lines.

ENDFORM.                    "frm_prepare_header

*&#038;--------------------------------------------------------------------*
*&#038;      Form  pf_status
*&#038;--------------------------------------------------------------------*
FORM frm_pf_status USING pv_button1 STRUCTURE smp_dyntxt
                         pv_button2 STRUCTURE smp_dyntxt
                         pv_button3 STRUCTURE smp_dyntxt
                         pv_button4 STRUCTURE smp_dyntxt
                         pv_button5 STRUCTURE smp_dyntxt
                         pr_extab   TYPE slis_t_extab
                         pr_title   TYPE sytitle  .   "EC *

  gt_extab = pr_extab.
* Activate / De-activate Button 1
  IF pv_button1 IS INITIAL.
    APPEND 'BUTTON1' TO gt_extab.
  ELSE.
    gw_button1 = pv_button1.
    DELETE gt_extab WHERE fcode EQ 'BUTTON1'.
  ENDIF.
* Activate / De-activate Button 2
  IF pv_button2 IS INITIAL.
    APPEND 'BUTTON2' TO gt_extab.
  ELSE.
    gw_button2 = pv_button2.
    DELETE gt_extab WHERE fcode EQ 'BUTTON2'.
  ENDIF.
* Activate / De-activate Button 3
  IF pv_button3 IS INITIAL.
    APPEND 'BUTTON3' TO gt_extab.
  ELSE.
    gw_button3 = pv_button3.
    DELETE gt_extab WHERE fcode EQ 'BUTTON3'.
  ENDIF.
* Activate / De-activate Button 4
  IF pv_button4 IS INITIAL.
    APPEND 'BUTTON4' TO gt_extab.
  ELSE.
    gw_button4 = pv_button4.
    DELETE gt_extab WHERE fcode EQ 'BUTTON4'.
  ENDIF.
* Activate / De-activate Button 5
  IF pv_button5 IS INITIAL.
    APPEND 'BUTTON5' TO gt_extab.
  ELSE.
    gw_button5 = pv_button5.
    DELETE gt_extab WHERE fcode EQ 'BUTTON5'.
  ENDIF.

* Activate / De-activate Button 6 if ALV header exceeds 25 lines
  IF gv_header_lines LT cn_$alv_hd.
    APPEND 'BUTTON6' TO gt_extab.
  ELSE.
    gv_show_bt6          = cn_$cross.
    gw_button6-icon_id   = '@7B@'.
    gw_button6-icon_text = text-s20.
    gw_button6-quickinfo = text-s20.
    DELETE gt_extab WHERE fcode EQ 'BUTTON6'.
  ENDIF.

* Force show Button6 to display selection parameters
  IF gv_show_bt6 IS INITIAL.
    APPEND 'BUTTON6' TO gt_extab.
  ELSE.
    gw_button6-icon_id   = '@7B@'.
    gw_button6-icon_text =
    gw_button6-quickinfo = text-s20.
    DELETE gt_extab WHERE fcode EQ 'BUTTON6'.
  ENDIF.

  SET PF-STATUS 'STANDARD' EXCLUDING gt_extab.
  SET TITLEBAR 'TITLE' WITH  pr_title .


ENDFORM.                    "PF_STATUS

*&#038;---------------------------------------------------------------------*
*&#038;      Form  frm_format_val
*&#038;---------------------------------------------------------------------*
*       Format values
*----------------------------------------------------------------------*
FORM frm_format_val  USING   pr_temp_sel TYPE ty_psel
                                 pr_dtyp TYPE char4.

  CASE pr_dtyp.
    WHEN 'NUMC' OR 'INT1'.
      IF NOT pr_temp_sel-low IS INITIAL.
        pr_temp_sel-low  = gv_char = gv_number = pr_temp_sel-low.
        IF NOT gv_number IS INITIAL.
          CONDENSE pr_temp_sel-low.
        ENDIF.
      ENDIF.
      IF NOT pr_temp_sel-high IS INITIAL.
        gv_char = gv_number = pr_temp_sel-high.
        IF NOT gv_number IS INITIAL.
          pr_temp_sel-high = gv_char.
          CONDENSE pr_temp_sel-high.
        ELSE.
          CLEAR pr_temp_sel-high.
        ENDIF.
      ENDIF.
* Initialize lower range to zero if higher range is entered
      IF pr_temp_sel-low  EQ 0 AND
         pr_temp_sel-high IS INITIAL.
        CLEAR pr_temp_sel-low.
      ENDIF.
    WHEN 'DEC'.
*      MOVE pr_temp_sel-low TO <fs> .
      IF NOT pr_temp_sel-low IS INITIAL. " AND
*         NOT pr_temp_sel-low = '0.00'.
        gv_decimal = pr_temp_sel-low.
        IF NOT gv_decimal IS INITIAL.
          CONDENSE pr_temp_sel-low.
        ENDIF.
      ENDIF.
*      MOVE pr_temp_sel-high TO <fs> .
*      IF NOT <fs> IS INITIAL.
      IF NOT pr_temp_sel-high IS INITIAL. " AND
*         NOT pr_temp_sel-high = '0.00'.
        gv_decimal = pr_temp_sel-high.
        IF NOT gv_decimal IS INITIAL.
          CONDENSE pr_temp_sel-high.
        ELSE.
          CLEAR pr_temp_sel-high.
        ENDIF.
      ENDIF.
    WHEN 'DATE'.
      IF NOT pr_temp_sel-low IS INITIAL.
        datum = pr_temp_sel-low.
        WRITE datum TO gv_date DD/MM/YYYY.
        pr_temp_sel-low = gv_date.
      ENDIF.
      IF NOT pr_temp_sel-high IS INITIAL.
        datum = pr_temp_sel-high.
        IF NOT datum IS INITIAL.
          WRITE datum TO gv_date DD/MM/YYYY.
          pr_temp_sel-high = gv_date.
        ELSE.
          CLEAR pr_temp_sel-high.
        ENDIF.
      ENDIF.
* Initialize lower range to zero if higher range is entered
      IF pr_temp_sel-low  EQ cn_$null_dt AND
         pr_temp_sel-high IS INITIAL.
        CLEAR: pr_temp_sel-option,
               pr_temp_sel-low.
      ENDIF.
  ENDCASE.

ENDFORM.                    " frm_format_val
</pre>
<p>&nbsp;</p>
<p><b>3.创建 <span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="Function Module" target="_blank">Function Module</a></span>，并激活</b></p>
<pre class="brush: abap;">

FUNCTION zca_alv_print_selection.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(REPID) TYPE  REPID
*"     REFERENCE(TITLE) TYPE  SYTITLE OPTIONAL
*"     REFERENCE(TFILL) TYPE  CHAR8 OPTIONAL
*"     REFERENCE(LOGO) TYPE  CHAR30 OPTIONAL
*"     REFERENCE(ALV_HEADER) TYPE  FLAG DEFAULT 'X'
*"     REFERENCE(TIMER_DATA) TYPE  FLAG OPTIONAL
*"     REFERENCE(PRINT_FLAG) TYPE  FLAG OPTIONAL
*"     REFERENCE(PARAM_FLAG) TYPE  FLAG OPTIONAL
*"     REFERENCE(DONT_SHOW_INITIAL) TYPE  CHAR1 DEFAULT 'X'
*"     REFERENCE(NO_CONF) TYPE  XFELD OPTIONAL
*"  EXPORTING
*"     REFERENCE(DATABEG) TYPE  BEGDA
*"     REFERENCE(DATAEND) TYPE  ENDDA
*"  TABLES
*"      T_SEL OPTIONAL
*"      T_SEL_DYN OPTIONAL
*"      T_HEADER OPTIONAL
*"      T_HEADER_ADD OPTIONAL
*"----------------------------------------------------------------------

  gv_show_bt6 = param_flag.
  DATA :
    lv_entry TYPE textpooltx.
  DATA:
    lw_l_selctab TYPE rsscr,
    lv_low TYPE char10,
    lv_high TYPE char10,
    lv_date_low TYPE sy-datum,
    lv_date_high TYPE sy-datum.

* For preview and re-visit options
  IF NOT gt_temp_sel[] IS INITIAL.
    DESCRIBE TABLE gt_header LINES gv_header_lines.
    IF gv_header_lines GT cn_$alv_hd AND NOT print_flag IS INITIAL.
      CLEAR gt_prn_header[].
      LOOP AT gt_header INTO gw_line FROM 1 TO cn_$alv_hd.
        APPEND gw_line TO gt_prn_header.
      ENDLOOP.
      CLEAR gw_line.
      gw_line-typ  = cn_types.   "s
      gw_line-info = text-s24.
      APPEND gw_line TO gt_prn_header.
      CLEAR gw_line.
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          i_logo             = logo
          it_list_commentary = gt_prn_header.
    ELSE.
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          i_logo             = logo
          it_list_commentary = gt_header.
    ENDIF.
    EXIT.
  ENDIF.

* Selection Screen values
  CALL FUNCTION 'RS_REFRESH_FROM_SELECTOPTIONS'
    EXPORTING
      curr_report     = repid
    TABLES
      selection_table = gt_sel_tab
    EXCEPTIONS
      OTHERS          = 0.

** Program Selections
  LOAD REPORT repid PART cn_part INTO gt_l_selctab. "SSCR
  READ TEXTPOOL repid INTO gt_pg_txtpool LANGUAGE sy-langu .
  LOOP AT gt_l_selctab.
    CLEAR:gt_pg_txtpool.
    READ TABLE gt_pg_txtpool WITH KEY id = cn_types
                                      key = gt_l_selctab-name.
    IF sy-subrc = 0.
      lv_entry = gt_pg_txtpool-entry.
      LOOP AT gt_sel_tab WHERE selname EQ gt_pg_txtpool-key.
        gt_pg_txtpool-entry = lv_entry.
        gt_temp_sel-slnam  = gt_pg_txtpool-key.
        IF gt_pg_txtpool-entry(1) = cn_typed."'D'.
          PERFORM get_dd_text(rsdbspdd) USING gt_l_selctab
                                        CHANGING gt_temp_sel-text.
        ELSE.
          SHIFT gt_pg_txtpool-entry BY 8 PLACES.
          IF gt_pg_txtpool-entry = cn_entry. "'?...'.
            gt_temp_sel-text = gt_sel_tab-selname.
          ELSE.
            gt_temp_sel-text   = gt_pg_txtpool-entry.
          ENDIF.
        ENDIF.

        CLEAR: lv_low,lv_high,lv_date_low,lv_date_high.
        CASE gt_l_selctab-dtyp .

          WHEN cn_dtype_d.   "'DATE'.
            gt_temp_sel-kind   = gt_sel_tab-kind.
            gt_temp_sel-sign   = gt_sel_tab-sign.
            gt_temp_sel-option = gt_sel_tab-option.
            lv_date_low  = gt_sel_tab-low.
            IF lv_date_low CO '0 '.
              gt_temp_sel-low = gt_sel_tab-low.
            ELSE.
              WRITE  lv_date_low  TO lv_low.
              gt_temp_sel-low    = lv_low.
            ENDIF.

            lv_date_high = gt_sel_tab-high.
            IF lv_date_high CO '0 '.
            ELSE.
              WRITE  lv_date_high TO lv_high.
              gt_temp_sel-high   = lv_high.
            ENDIF.
          WHEN cn_dtype_n . "'NUMC'.
            gt_temp_sel-kind   = gt_sel_tab-kind.
            gt_temp_sel-sign   = gt_sel_tab-sign.
            gt_temp_sel-option = gt_sel_tab-option.
            gt_temp_sel-low    = gt_sel_tab-low.

            IF gt_sel_tab-high CO '0 '.
            ELSE.
              gt_temp_sel-high   = gt_sel_tab-high.
            ENDIF.

          WHEN OTHERS.
            gt_temp_sel-kind   = gt_sel_tab-kind.
            gt_temp_sel-sign   = gt_sel_tab-sign.
            gt_temp_sel-option = gt_sel_tab-option.
            gt_temp_sel-low    = gt_sel_tab-low.
            gt_temp_sel-high   = gt_sel_tab-high.
        ENDCASE.

        CONDENSE: gt_temp_sel-text,
                  gt_temp_sel-low,
                  gt_temp_sel-high.
        add_itab gt_temp_sel.
      ENDLOOP.
    ENDIF.
    CLEAR:gt_l_selctab.
  ENDLOOP.


* Prepare ALV Header, if required
  PERFORM frm_prepare_header TABLES t_header_add
                                    t_sel
                                    t_sel_dyn
                                    t_header
                              USING title repid tfill
                                    logo alv_header dont_show_initial
                                    no_conf.
  databeg = gv_databeg.
  dataend = gv_dataend.

ENDFUNCTION.

</pre>
<p>&nbsp;</p>
<p><b>4.创建 ALV Template Include 文件</b></p>
<pre class="brush: abap;">

*&#038;---------------------------------------------------------------------*
*&#038;  Include           ZCA_ALVTMP_T01
*&#038;---------------------------------------------------------------------*
* Descripion:  Include to display selection screen data in
*              ALV grid/list output report headers
* Note: Should have logical database assigned to the main program
*&#038;---------------------------------------------------------------------*
*                     Modify Log History.
*----------------------------------------------------------------------*
*
*No. Modified by      Date          Description
*--- -----------      ----          -----------
*M1
*----------------------------------------------------------------------*
TYPE-POOLS:slis.

TYPES:
  BEGIN OF ty_s_psel,
    kind     TYPE flag,
    slnam    TYPE char8,
    text     TYPE char255,
    sign     TYPE tvarv_sign,
    option   TYPE tvarv_opti,
    low      TYPE tvarv_val,
    high     TYPE tvarv_val,
  END OF ty_s_psel.

*** Constant definition
CONSTANTS:
  cn_cross   TYPE flag     VALUE 'X',
  cn_zero_dt TYPE datum    VALUE '00000000',
  cn_header  TYPE char30   VALUE 'FRM_PAGE_HEADER',
  cn_ucomm   TYPE char30   VALUE 'FRM_USER_COMMAND',
  cn_stat    TYPE char30   VALUE 'FRM_PF_STATUS',
  cn_zero                  TYPE i  VALUE 0,
  cn_asterisk              TYPE c  VALUE '*',
  cn_dash                  TYPE c  VALUE '-',
  cn_s_y                   TYPE string VALUE 'SY',
  cn_p_rint                TYPE string VALUE 'PRIN',
  cn_prev                  TYPE string VALUE '&#038;RNT_PREV' ,
  cn_line_typ_s            TYPE string VALUE 'S',
  cn_dot                   TYPE string VALUE '.',
  cn_quantity              TYPE string VALUE 'Q',
  cn_currency              TYPE string VALUE 'C',
  cn_didtxt_l              TYPE string VALUE 'L',
  cn_save_a                TYPE string VALUE 'A',
  cn_topofpage               TYPE string VALUE 'TOP_OF_PAGE',
  cn_fr_pg_hd                TYPE string VALUE 'FRM_PAGE_HEADER',
  cn_endoflist               TYPE string VALUE 'END_OF_LIST',
  cn_fr_pg_ft                TYPE string VALUE 'FRM_PAGE_FOOTER',


** ALV Table Begin Tag Name,
*  All that use this include must follow the pattern.
  cn_alv_tab_beg_tag(14)   TYPE c  VALUE '*$BEG_GT_PRINT',
  cn_alv_tab_end_tag(14)   TYPE c  VALUE '*$END_GT_PRINT',

  cn_r01 TYPE string VALUE '([A-Za-z0-9_]+-[A-Za-z0-9_]+)', "#EC *
  cn_r02 TYPE string VALUE '([^ ]+)([ ]+)TYPE',             "#EC *
  cn_r03 TYPE string VALUE '("........\|)',                 "#EC *
  cn_r04 TYPE string VALUE '(\|.+\|)',                      "#EC *
  cn_r05 TYPE string VALUE '(\|([CQ]_[A-Za-z0-9_]+)")'.     "#EC *

*** varibale definition
DATA:
  gv_repid         TYPE syrepid,
  gv_repid_scr     TYPE syrepid,
  gv_tfill         TYPE char8,
  gv_title         TYPE sytitle,
  gv_alv_header    TYPE flag VALUE 'X',
  gv_param_flag    TYPE flag,
  gv_print_flag    TYPE flag,
  gv_logo          TYPE char30,
  gv_ucomm         TYPE slis_formname,
  gv_noconf        TYPE xfeld,
  gv_noinitial     TYPE char1 VALUE 'X',
  gv_post_check     TYPE char1.





*** work area and internal table definition
DATA:
  gw_layout        TYPE slis_layout_alv,
  gt_fieldcat      TYPE slis_t_fieldcat_alv,
  gw_fieldcat      TYPE slis_fieldcat_alv,
  gt_sort          TYPE slis_t_sortinfo_alv,
  gw_sort          TYPE slis_sortinfo_alv,
  gt_events        TYPE slis_t_event,
  gw_events        TYPE slis_alv_event,
  gt_header        TYPE slis_t_listheader,
  gt_header_add    TYPE TABLE OF ty_s_psel,
  gt_pf_exclude    TYPE slis_t_extab,
  gw_pf_exclude    TYPE slis_extab,
  gw_variant       TYPE disvariant,
  gt_add_fieldcat  TYPE slis_t_add_fieldcat,
  gw_add_fieldcat  TYPE slis_add_fieldcat.

DATA:
  gw_button1       TYPE smp_dyntxt,
  gw_button2       TYPE smp_dyntxt,
  gw_button3       TYPE smp_dyntxt,
  gw_button4       TYPE smp_dyntxt,
  gw_button5       TYPE smp_dyntxt,
  gv_variant       TYPE variant.

*** Work area and internal table  define for LVC moduel
DATA:
  gv_lvc_mode      TYPE char1 VALUE space, " 'X' - enabled
  gw_lvc_layout    TYPE lvc_s_layo,
  gt_lvc_fcat      TYPE STANDARD TABLE OF lvc_s_fcat,
  gt_sort_lvc      TYPE STANDARD TABLE OF lvc_s_sort,
  gw_lvc_fcat      TYPE lvc_s_fcat,
  gw_lvc_sort      TYPE lvc_s_sort,
  gt_event_exit    TYPE slis_t_event_exit.


*&#038;---------------------------------------------------------------------*
*&#038;      Form  FRM_CLEAR_VARIABLE
*&#038;---------------------------------------------------------------------*
*       Clear variable
*----------------------------------------------------------------------*
FORM frm_clear_variable .

  CLEAR: gv_repid,
         gv_repid_scr,
         gv_tfill,
         gv_title,
         gv_param_flag,
         gv_print_flag,
         gv_logo,
         gv_ucomm,
         gv_noconf ,
         gw_variant,
         gw_lvc_layout,
         gv_alv_header,
         gv_noinitial.
  REFRESH: gt_header_add,
           gt_sort_lvc.

ENDFORM.                    " FRM_CLEAR_VARIABLE

*&#038;---------------------------------------------------------------------*
*&#038;      Form  FRM_PAGE_HEADER
*&#038;---------------------------------------------------------------------*
FORM frm_page_header.                                       "#EC *
  DATA : lv_repid TYPE syrepid,
         lv_title TYPE sytitle.
* Check Print out complete. If complete, clear ucomm
  IF sy-msgid EQ cn_s_y AND sy-ucomm EQ cn_p_rint.
    CLEAR sy-ucomm.
  ENDIF.
  IF sy-ucomm EQ cn_prev OR sy-ucomm EQ cn_p_rint.
    gv_print_flag = cn_cross.
  ELSE.
    CLEAR gv_print_flag.
  ENDIF.
*** If you want to call Selection screen of some other program,
*** fill REPID_SCR.
  IF gv_repid_scr IS INITIAL.
    lv_repid = gv_repid.
  ELSE.
    lv_repid = gv_repid_scr.
  ENDIF.

*** set different title IN ALV header
  IF gv_title IS NOT INITIAL.
    lv_title = gv_title.
  ELSE.
    lv_title = sy-title.
  ENDIF.

*** set print header

  CALL FUNCTION 'ZCA_ALV_PRINT_SELECTION'
    EXPORTING
      repid             = lv_repid
      title             = lv_title
      tfill             = gv_tfill
      logo              = gv_logo
      alv_header        = gv_alv_header
      timer_data        = space
      print_flag        = gv_print_flag
      param_flag        = gv_param_flag
      no_conf           = gv_noconf
      dont_show_initial = gv_noinitial
    TABLES
      t_header          = gt_header
      t_header_add      = gt_header_add.

ENDFORM.                    " FRM_PAGE_HEADER

*&#038;--------------------------------------------------------------------*
*&#038;      Form  frm_page_footer
*&#038;--------------------------------------------------------------------*
FORM frm_page_footer.                                       "#EC called

  DATA: lt_footer TYPE slis_t_listheader,
        lw_line   TYPE slis_listheader.

  lw_line-typ  = cn_line_typ_s.
  lw_line-key  = cn_dot.
  lw_line-info = cn_dot.
  APPEND lw_line TO lt_footer.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = lt_footer
      i_end_of_list_grid = cn_cross.

ENDFORM.                    "frm_page_footer
*&#038;--------------------------------------------------------------------*
*&#038;      Form  frm_event_build
*&#038;--------------------------------------------------------------------*
FORM frm_event_build.                                       "#EC CALLED

  gw_events-name = cn_topofpage. "'TOP_OF_PAGE'.
  gw_events-form = cn_fr_pg_hd.  "'FRM_PAGE_HEADER'.
  APPEND gw_events TO gt_events.
*
  gw_events-name = cn_endoflist. "'END_OF_LIST'.
  gw_events-form = cn_fr_pg_ft.  "'FRM_PAGE_FOOTER'.
  APPEND gw_events TO gt_events.
*
ENDFORM.                    "frm_event_build
*&#038;--------------------------------------------------------------------*
*&#038;      Form  frm_pf_status
*&#038;--------------------------------------------------------------------*
FORM frm_pf_status USING pr_t_extab TYPE slis_t_extab.      "#EC *

  DATA:
    lv_title TYPE sytitle.

*** exclud button
  pr_t_extab = gt_pf_exclude.

*** set different title in title bar
  IF gv_title IS NOT INITIAL.
    lv_title = gv_title.
  ELSE.
    lv_title = sy-title.
  ENDIF.

*** set PF-STATUS and TITLE BAR
  PERFORM frm_pf_status IN PROGRAM saplzca_alv_print_selection IF FOUND
                                     USING gw_button1
                                           gw_button2
                                           gw_button3
                                           gw_button4
                                           gw_button5
                                           pr_t_extab
                                           lv_title.


ENDFORM.                    "PF_STATUS

*&#038;---------------------------------------------------------------------*
*&#038;      Form  FRM_RETRIEVE_FIELDS
*&#038;---------------------------------------------------------------------*
*       Build Field Catalog
*----------------------------------------------------------------------*
*      -->LC_ALV_TAB_NAME  ALV Table Tag Name
*----------------------------------------------------------------------*
FORM frm_retrieve_fields  USING  pr_alv_tab_name TYPE string.
  DATA:
*   Match offset
    lv_moff              TYPE sy-tabix,
*   Match Length
    lv_mlen              TYPE sy-tabix,
*   Table definition start index
    lv_alv_beg_tabix TYPE sy-tabix,
*   Table definition end index
    lv_alv_end_tabix TYPE sy-tabix.
*** For field catalog
  DATA:
    lt_source TYPE TABLE OF prk_source.

  DATA:
    lw_source  TYPE prk_source.

  READ REPORT sy-repid INTO lt_source.                      "#EC *
  LOOP AT lt_source INTO lw_source WHERE LINE = cn_alv_tab_beg_tag OR
                                          LINE = cn_alv_tab_end_tag.
    IF lw_source-line = cn_alv_tab_beg_tag.
      lv_alv_beg_tabix = sy-tabix.
    ELSEIF lw_source-line = cn_alv_tab_end_tag.
      lv_alv_end_tabix = sy-tabix.
    ENDIF.
  ENDLOOP.

  lv_alv_beg_tabix = lv_alv_beg_tabix + 1.
  lv_alv_end_tabix = lv_alv_end_tabix - 1.

  REFRESH gt_fieldcat.
  LOOP AT lt_source INTO lw_source FROM lv_alv_beg_tabix
                                    TO   lv_alv_end_tabix.
* Avoid Comment parsing
    IF lw_source-line(1) NE cn_asterisk.

***********************************************************************
* Change all before " to Upper Case
***********************************************************************
      FIND FIRST OCCURRENCE OF '"' IN lw_source-line
      MATCH OFFSET lv_moff.
      IF sy-subrc = 0.
        TRANSLATE lw_source-line(lv_moff) TO UPPER CASE.
      ENDIF.

      CLEAR gw_fieldcat.

      gw_fieldcat-qtabname =
      gw_fieldcat-ctabname =
      gw_fieldcat-tabname  = pr_alv_tab_name.

***********************************************************************
* Find 2 Words Connected by dash
***********************************************************************
      FIND REGEX cn_r01 IN lw_source-line
      MATCH OFFSET lv_moff
      MATCH LENGTH lv_mlen.
      IF sy-subrc = cn_zero.
        SPLIT lw_source-line+lv_moff(lv_mlen) AT cn_dash
        INTO gw_fieldcat-ref_tabname
             gw_fieldcat-ref_fieldname.
      ENDIF.
***********************************************************************
* Find field name before the word TYPE
***********************************************************************
      FIND REGEX cn_r02 IN lw_source-line
      MATCH OFFSET lv_moff
      MATCH LENGTH lv_mlen.

      IF sy-subrc = cn_zero.
* Exclude the word 'TYPE' 4 characters
        lv_mlen = lv_mlen - 4.
        gw_fieldcat-fieldname = lw_source-line+lv_moff(lv_mlen).
        CONDENSE gw_fieldcat-fieldname.
      ENDIF.
***********************************************************************
* Find 5 digit of single character fields
***********************************************************************
      FIND REGEX cn_r03 IN lw_source-line
      MATCH OFFSET lv_moff
      MATCH LENGTH lv_mlen.

      IF sy-subrc = cn_zero.
* Exclude the " and |
        lv_moff = lv_moff + 1.
        gw_fieldcat-just      = lw_source-line+lv_moff.

        lv_moff = lv_moff + 1.
        gw_fieldcat-no_zero   = lw_source-line+lv_moff.

        lv_moff = lv_moff + 1.
        gw_fieldcat-edit      = lw_source-line+lv_moff.

        lv_moff = lv_moff + 1.
        gw_fieldcat-do_sum    = lw_source-line+lv_moff.

        lv_moff = lv_moff + 1.
        gw_fieldcat-no_out    = lw_source-line+lv_moff.

        lv_moff = lv_moff + 1.
        gw_fieldcat-fix_column =
        gw_fieldcat-key        = lw_source-line+lv_moff.

        lv_moff = lv_moff + 1.
        gw_fieldcat-icon      = lw_source-line+lv_moff.

        lv_moff = lv_moff + 1.
        gw_fieldcat-lowercase = lw_source-line+lv_moff.

      ENDIF.

***********************************************************************
* Find Description
***********************************************************************
      FIND REGEX cn_r04 IN lw_source-line
      MATCH OFFSET lv_moff
      MATCH LENGTH lv_mlen.

      IF sy-subrc = cn_zero.
* Exclude the front | and |
        lv_moff = lv_moff + 1.
        lv_mlen = lv_mlen - 2.

        gw_fieldcat-seltext_s = gw_fieldcat-seltext_m =
        gw_fieldcat-seltext_l = gw_fieldcat-reptext_ddic =
        lw_source-line+lv_moff(lv_mlen).
        gw_fieldcat-ddictxt = cn_didtxt_l.  " 'L'
        CONDENSE: gw_fieldcat-seltext_m, gw_fieldcat-seltext_l,
                  gw_fieldcat-seltext_s.
      ENDIF.

***********************************************************************
* Find Quantity or Currency Reference
***********************************************************************
      FIND REGEX cn_r05 IN lw_source-line
      MATCH OFFSET lv_moff
      MATCH LENGTH lv_mlen.

      IF sy-subrc = cn_zero.
* Exclude the | and "
        lv_moff = lv_moff + 1.
        lv_mlen = lv_mlen - 4.

        IF lw_source-line+lv_moff(1) EQ cn_quantity.  "'Q'.
          lv_moff = lv_moff + 2.
          gw_fieldcat-qfieldname = lw_source-line+lv_moff(lv_mlen).
        ELSEIF lw_source-line+lv_moff(1) EQ cn_currency.  "'C'.
          lv_moff = lv_moff + 2.
          gw_fieldcat-cfieldname = lw_source-line+lv_moff(lv_mlen).
        ENDIF.
      ENDIF.

    ENDIF.
    APPEND gw_fieldcat TO gt_fieldcat.
    CLEAR: gw_fieldcat.
  ENDLOOP.

*** Build LVC field catlog
  IF gv_lvc_mode = cn_cross.

    CLEAR gw_fieldcat.
    LOOP AT gt_fieldcat INTO gw_fieldcat.

      MOVE-CORRESPONDING gw_fieldcat TO gw_lvc_fcat.        "#EC *

      MOVE:
        gw_fieldcat-seltext_l      TO   gw_lvc_fcat-seltext,
        gw_fieldcat-seltext_l      TO   gw_lvc_fcat-coltext,
        gw_fieldcat-ref_fieldname  TO   gw_lvc_fcat-ref_field,
        gw_fieldcat-ref_tabname    TO   gw_lvc_fcat-ref_table
        .

      APPEND gw_lvc_fcat TO gt_lvc_fcat.
      CLEAR:
        gw_fieldcat,gw_lvc_fcat.
    ENDLOOP.

  ENDIF.

ENDFORM.                    " FRM_RETRIEVE_FIELDS   "#EC *
*&#038;---------------------------------------------------------------------*
*&#038;      Form  frm_show_alv_rep
*&#038;---------------------------------------------------------------------*
*       Show ALV Report
*** T_DATA is the data
*** layout is the layout
*** field is the field catalog
*----------------------------------------------------------------------*
FORM frm_show_alv_rep TABLES pr_t_data.                     "#EC called
*COLL_TOP_P
  DATA : lw_grid_set TYPE lvc_s_glay,
         lv_save TYPE char1,
         lv_header TYPE char30.

  DESCRIBE TABLE pr_t_data LINES gv_tfill.
  CHECK gv_tfill > 0.

  gv_repid = sy-repid.
*** Variant
  IF NOT gw_variant IS INITIAL.
    lv_save = cn_save_a. " 'A'.
  ELSE.
    CLEAR: lv_save.
    gw_variant-report = sy-repid.
  ENDIF.
*  grid_set-coll_top_p = '1'.
  IF gv_ucomm IS INITIAL.
    IF NOT gw_button1 IS INITIAL OR
       NOT gw_button2 IS INITIAL OR
       NOT gw_button3 IS INITIAL OR
       NOT gw_button4 IS INITIAL OR
       NOT gw_button5 IS INITIAL.
      gv_ucomm = cn_ucomm.
    ENDIF.
  ENDIF.

  IF gv_lvc_mode = cn_cross.   " LVC mode

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
      EXPORTING
*       I_INTERFACE_CHECK                 = ' '
*       I_BYPASSING_BUFFER                = I_BYPASSING_BUFFER
*       I_BUFFER_ACTIVE                   = I_BUFFER_ACTIVE
        i_callback_program                = gv_repid
        i_callback_pf_status_set          = cn_stat
        i_callback_user_command           = gv_ucomm
        i_callback_top_of_page            = cn_header
*       I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*       I_CALLBACK_HTML_END_OF_LIST       = ' '
*       I_STRUCTURE_NAME                  = I_STRUCTURE_NAME
*       I_BACKGROUND_ID                   = ' '
*       I_GRID_TITLE                      = I_GRID_TITLE
*       I_GRID_SETTINGS                   = I_GRID_SETTINGS
        is_layout_lvc                     = gw_lvc_layout
        it_fieldcat_lvc                   = gt_lvc_fcat
        it_excluding                      = gt_pf_exclude
*       IT_SPECIAL_GROUPS_LVC             = IT_SPECIAL_GROUPS_LVC
        it_sort_lvc                       = gt_sort_lvc
*       IT_FILTER_LVC                     = IT_FILTER_LVC
*       IT_HYPERLINK                      = IT_HYPERLINK
*       IS_SEL_HIDE                       = IS_SEL_HIDE
*       I_DEFAULT                         = 'X'
        i_save                            = lv_save
        is_variant                        = gw_variant
        it_events                         = gt_events
        it_event_exit                     = gt_event_exit
*       IS_PRINT_LVC                      = IS_PRINT_LVC
*       IS_REPREP_ID_LVC                  = IS_REPREP_ID_LVC
*       I_SCREEN_START_COLUMN             = 0
*       I_SCREEN_START_LINE               = 0
*       I_SCREEN_END_COLUMN               = 0
*       I_SCREEN_END_LINE                 = 0
        i_html_height_top                 = '91'
*       I_HTML_HEIGHT_END                 = I_HTML_HEIGHT_END
*       IT_ALV_GRAPHICS                   = IT_ALV_GRAPHICS
*       IT_EXCEPT_QINFO_LVC               = IT_EXCEPT_QINFO_LVC
*       IR_SALV_FULLSCREEN_ADAPTER        = IR_SALV_FULLSCREEN_ADAPTER
      TABLES
        t_outtab                          = pr_t_data
      EXCEPTIONS
        program_error                     = 1
        OTHERS                            = 2.

    IF sy-subrc = 0.
*     Call function successful
    ENDIF.

  ELSE.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program       = gv_repid
*        i_callback_top_of_page   = cn_header "l_header
        i_callback_user_command  = gv_ucomm
        i_callback_pf_status_set = cn_stat
        it_events                = gt_events
        it_event_exit            = gt_event_exit
*        i_grid_settings          = grid_set
        is_layout                = gw_layout
        it_fieldcat              = gt_fieldcat
        it_sort                  = gt_sort
*        i_default                = l_default
        is_variant                = gw_variant
        i_save                   = lv_save
        it_excluding             = gt_pf_exclude
*        i_html_height_top        = '91'
        it_add_fieldcat          = gt_add_fieldcat
      TABLES
        t_outtab                 = pr_t_data
      EXCEPTIONS
        OTHERS                   = 0.

  ENDIF.

ENDFORM.                    "frm_show_alv_rep
</pre>
<p>&nbsp;</p>
<p>如若要使用ALV 模板，直接引入<b>ZCA_ALVTMP_T01</b> Include文件即可。</p>
<p>&nbsp;</p>
<p><b>5.使用 ALV Template 开发 Report 示例</b></p>
<p>这里给出个简单的Demo：</p>
<pre class="brush: abap;">

REPORT zalv_demo.
************************************************************************
*                  I N C L U D E                                       *
************************************************************************
INCLUDE zca_alvtmp_t01.    "ALV Template
************************************************************************
*                  T Y P E S                                           *
************************************************************************
* types :
****** Example Only for ALV. Remove all these lines if your program is
********** not using ALV.
********** For automatic building of fieldcatalog,
********** declare the ALV internal table as follows:
* 8 Characters after the first quote(") refers to:
* [Justification][No zero][Edit][Do Sum][No Out][Key][Icon][Lower Case]

TYPES:
BEGIN OF ty_s_print,
*$BEG_GT_PRINT
  check TYPE char01,        "LXX     |选择|"
  seqno TYPE i,             "LX      |序号|"
  werks TYPE mseg-werks,    "LX      |工厂|"
  mblnr TYPE mseg-mblnr,    "LX      |物料凭证|"
  matnr TYPE mseg-matnr,    "LX      |内部品番|"
*$END_GT_PRINT
END OF ty_s_print.

TYPE-POOLS: icon,abap.

************************************************************************
*                  V A R I A B L E                                     *
************************************************************************

DATA: gv_nodataflag TYPE c, "flag for no data found.
      gv_seqno TYPE i .

************************************************************************
*                  I N N E R  T A B L E  A N D  W O R K A R E A        *
************************************************************************
DATA:
***internal table
      gt_print TYPE STANDARD TABLE OF ty_s_print. "ALV输出表


DATA:
***work area for ALV
      gw_print LIKE LINE OF gt_print.

************************************************************************
*                  C O N S T A N T S                                   *
************************************************************************

CONSTANTS:
*** Uncomment this line to pass the ALV Tab name
      cn_alv_tab_name   TYPE string VALUE 'GT_PRINT',
      cn_msgtyp         TYPE c      VALUE 'E',  "Error message Type.
      cn_flagx          TYPE c      VALUE 'X',  "Value X
      cn_operation      TYPE c      VALUE ';',  "spilt char
      cn_slash(1)       TYPE c      VALUE '/',
      cn_vgart(2)       TYPE c      VALUE 'WF' ,"Transaction type
      cn_fc_print(10)   TYPE c      VALUE 'BUTTON1',
      cn_fc_sall_n(10)  TYPE c      VALUE 'BUTTON2',
      cn_fc_dsall_n(10) TYPE c      VALUE 'BUTTON3',
      cn_fc_sv(10)      TYPE c      VALUE 'SAVE',
      cn_check(10)      TYPE c      VALUE 'CHECK',
      cn_seqno(10)      TYPE c      VALUE 'SEQNO',
      cn_fc_click(10)   TYPE c      VALUE '&#038;IC1',
      cn_fc_sall(10)    TYPE c      VALUE '&#038;ALL',
      cn_fc_dsall(10)   TYPE c      VALUE '&#038;SAL'. 

************************************************************************
*                  S E L E C T I O N  S C R E E N                      *
************************************************************************
SELECTION-SCREEN: BEGIN OF BLOCK blk1 WITH FRAME TITLE text-t01.
SELECT-OPTIONS so_matnr FOR mseg-matnr.
SELECT-OPTIONS so_werks FOR mseg-werks. 
SELECTION-SCREEN: END OF BLOCK blk1.

************************************************************************
*                  I N I T I A L I Z A T I O N                         *
************************************************************************
INITIALIZATION.

  PERFORM frm_initialization.
*** Set customize buttons
  PERFORM frm_set_buttons.
*** Call this subroutine for automatic building of fieldcatalog
*** based on your type definition.
  PERFORM frm_retrieve_fields USING cn_alv_tab_name.

  PERFORM frm_set_fieldcat.

************************************************************************
*                  S T A R T  O F  S E L E C T I O N                   *
************************************************************************
START-OF-SELECTION.
* Get data from databasse
  PERFORM frm_get_data.
* Data processing
  PERFORM frm_data_process USING gv_nodataflag.

************************************************************************
*                  E N D  O F  S E L E C T I O N                       *
************************************************************************
END-OF-SELECTION.
*** Display report as alv formart
  PERFORM frm_show_alv_rep TABLES gt_print.


*&#038;---------------------------------------------------------------------*
*&#038;      Form  FRM_INITIALIZATION
*&#038;---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_initialization .
*** set ALV layout
  gw_layout-colwidth_optimize = cn_flagx.

ENDFORM.                    "frm_initialization


*&#038;---------------------------------------------------------------------*
*&#038;      Form  frm_set_buttons
*&#038;---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_set_buttons .

  " Add button for update data after ALV output
  gw_button1-icon_id = icon_print.
  " cn_icon_upd stands for 'PRINT'
  gw_button1-icon_text = '打印'.
  " text-016 stands for 'Update data'
  gw_button1-quickinfo = '打印'.

  gw_button2-icon_id = icon_select_all.
  gw_button2-icon_text = '全选'.
  gw_button2-quickinfo = '全选'.

  gw_button3-icon_id = icon_deselect_all.
  gw_button3-icon_text = '反选'.
  gw_button3-quickinfo = '反选'.

  " Disable ALV standard save button, cn_fc_sv stands for 'SAVE'
  gw_pf_exclude-fcode = cn_fc_sv.
  APPEND gw_pf_exclude TO gt_pf_exclude.

  gw_pf_exclude-fcode = cn_fc_sall.
  APPEND gw_pf_exclude TO gt_pf_exclude.

  gw_pf_exclude-fcode = cn_fc_dsall.
  APPEND gw_pf_exclude TO gt_pf_exclude.
ENDFORM.                    " FRM_SET_BUTTONS


*&#038;---------------------------------------------------------------------*
*&#038;      Form  FRM_USER_COMMAND
*&#038;---------------------------------------------------------------------*
*       User command
*----------------------------------------------------------------------*
FORM frm_user_command USING pr_ucomm LIKE sy-ucomm
                         pr_selfield TYPE slis_selfield.

  DATA: lv_grid TYPE REF TO cl_gui_alv_grid.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lv_grid.

  CALL METHOD lv_grid->check_changed_data.

  pr_selfield-refresh = 'X'.  "自动刷新

  CASE pr_ucomm.
      " cn_fc_bt1 stands for 'BUTTON1'
    WHEN cn_fc_print.

      " Print out
      PERFORM frm_output_form.

    WHEN cn_fc_click.  "check box
      IF gw_print-check = space.
        gw_print-check = cn_flagx.
      ELSE.
        gw_print-check = space.
      ENDIF.
      MODIFY gt_print FROM gw_print INDEX pr_selfield-tabindex
      TRANSPORTING check.

    WHEN cn_fc_sall_n. "select all
      LOOP AT gt_print INTO gw_print.
        gw_print-check = cn_flagx.
        MODIFY gt_print FROM gw_print INDEX sy-tabix TRANSPORTING check.
      ENDLOOP.

    WHEN cn_fc_dsall_n. "deselect all
      LOOP AT gt_print INTO gw_print.
        gw_print-check = space.
        MODIFY gt_print FROM gw_print INDEX sy-tabix TRANSPORTING check.
      ENDLOOP.

    WHEN OTHERS.

  ENDCASE.
  " For refresh alv
  pr_selfield-refresh = abap_true.

ENDFORM.                    "USER_COMMAND


*&#038;---------------------------------------------------------------------*
*&#038;      Form  frm_set_fieldcat
*&#038;---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM frm_set_fieldcat .

  READ TABLE gt_fieldcat INTO gw_fieldcat WITH KEY fieldname = cn_check.
  IF sy-subrc = 0.
    gw_fieldcat-checkbox = cn_flagx.

    MODIFY gt_fieldcat FROM gw_fieldcat INDEX sy-tabix
    TRANSPORTING checkbox ."hotspot.
  ENDIF.
ENDFORM.                    " frm_set_fieldcat
</pre>
